/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import pojos.SubcontarctorResources;
import java.util.ArrayList;
import java.util.Collection;
import pojos.Subcontarctors;

/**
 *
 * @author A7med Kotb
 */
public class SubcontarctorsJpaController implements Serializable {

    public SubcontarctorsJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Subcontarctors subcontarctors) throws RollbackFailureException, Exception {
        if (subcontarctors.getSubcontarctorResourcesCollection() == null) {
            subcontarctors.setSubcontarctorResourcesCollection(new ArrayList<SubcontarctorResources>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Collection<SubcontarctorResources> attachedSubcontarctorResourcesCollection = new ArrayList<SubcontarctorResources>();
            for (SubcontarctorResources subcontarctorResourcesCollectionSubcontarctorResourcesToAttach : subcontarctors.getSubcontarctorResourcesCollection()) {
                subcontarctorResourcesCollectionSubcontarctorResourcesToAttach = em.getReference(subcontarctorResourcesCollectionSubcontarctorResourcesToAttach.getClass(), subcontarctorResourcesCollectionSubcontarctorResourcesToAttach.getSubResId());
                attachedSubcontarctorResourcesCollection.add(subcontarctorResourcesCollectionSubcontarctorResourcesToAttach);
            }
            subcontarctors.setSubcontarctorResourcesCollection(attachedSubcontarctorResourcesCollection);
            em.persist(subcontarctors);
            for (SubcontarctorResources subcontarctorResourcesCollectionSubcontarctorResources : subcontarctors.getSubcontarctorResourcesCollection()) {
                Subcontarctors oldSubcontractorOfSubcontarctorResourcesCollectionSubcontarctorResources = subcontarctorResourcesCollectionSubcontarctorResources.getSubcontractor();
                subcontarctorResourcesCollectionSubcontarctorResources.setSubcontractor(subcontarctors);
                subcontarctorResourcesCollectionSubcontarctorResources = em.merge(subcontarctorResourcesCollectionSubcontarctorResources);
                if (oldSubcontractorOfSubcontarctorResourcesCollectionSubcontarctorResources != null) {
                    oldSubcontractorOfSubcontarctorResourcesCollectionSubcontarctorResources.getSubcontarctorResourcesCollection().remove(subcontarctorResourcesCollectionSubcontarctorResources);
                    oldSubcontractorOfSubcontarctorResourcesCollectionSubcontarctorResources = em.merge(oldSubcontractorOfSubcontarctorResourcesCollectionSubcontarctorResources);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Subcontarctors subcontarctors) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Subcontarctors persistentSubcontarctors = em.find(Subcontarctors.class, subcontarctors.getSubId());
            Collection<SubcontarctorResources> subcontarctorResourcesCollectionOld = persistentSubcontarctors.getSubcontarctorResourcesCollection();
            Collection<SubcontarctorResources> subcontarctorResourcesCollectionNew = subcontarctors.getSubcontarctorResourcesCollection();
            List<String> illegalOrphanMessages = null;
            for (SubcontarctorResources subcontarctorResourcesCollectionOldSubcontarctorResources : subcontarctorResourcesCollectionOld) {
                if (!subcontarctorResourcesCollectionNew.contains(subcontarctorResourcesCollectionOldSubcontarctorResources)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain SubcontarctorResources " + subcontarctorResourcesCollectionOldSubcontarctorResources + " since its subcontractor field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<SubcontarctorResources> attachedSubcontarctorResourcesCollectionNew = new ArrayList<SubcontarctorResources>();
            for (SubcontarctorResources subcontarctorResourcesCollectionNewSubcontarctorResourcesToAttach : subcontarctorResourcesCollectionNew) {
                subcontarctorResourcesCollectionNewSubcontarctorResourcesToAttach = em.getReference(subcontarctorResourcesCollectionNewSubcontarctorResourcesToAttach.getClass(), subcontarctorResourcesCollectionNewSubcontarctorResourcesToAttach.getSubResId());
                attachedSubcontarctorResourcesCollectionNew.add(subcontarctorResourcesCollectionNewSubcontarctorResourcesToAttach);
            }
            subcontarctorResourcesCollectionNew = attachedSubcontarctorResourcesCollectionNew;
            subcontarctors.setSubcontarctorResourcesCollection(subcontarctorResourcesCollectionNew);
            subcontarctors = em.merge(subcontarctors);
            for (SubcontarctorResources subcontarctorResourcesCollectionNewSubcontarctorResources : subcontarctorResourcesCollectionNew) {
                if (!subcontarctorResourcesCollectionOld.contains(subcontarctorResourcesCollectionNewSubcontarctorResources)) {
                    Subcontarctors oldSubcontractorOfSubcontarctorResourcesCollectionNewSubcontarctorResources = subcontarctorResourcesCollectionNewSubcontarctorResources.getSubcontractor();
                    subcontarctorResourcesCollectionNewSubcontarctorResources.setSubcontractor(subcontarctors);
                    subcontarctorResourcesCollectionNewSubcontarctorResources = em.merge(subcontarctorResourcesCollectionNewSubcontarctorResources);
                    if (oldSubcontractorOfSubcontarctorResourcesCollectionNewSubcontarctorResources != null && !oldSubcontractorOfSubcontarctorResourcesCollectionNewSubcontarctorResources.equals(subcontarctors)) {
                        oldSubcontractorOfSubcontarctorResourcesCollectionNewSubcontarctorResources.getSubcontarctorResourcesCollection().remove(subcontarctorResourcesCollectionNewSubcontarctorResources);
                        oldSubcontractorOfSubcontarctorResourcesCollectionNewSubcontarctorResources = em.merge(oldSubcontractorOfSubcontarctorResourcesCollectionNewSubcontarctorResources);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = subcontarctors.getSubId();
                if (findSubcontarctors(id) == null) {
                    throw new NonexistentEntityException("The subcontarctors with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Subcontarctors subcontarctors;
            try {
                subcontarctors = em.getReference(Subcontarctors.class, id);
                subcontarctors.getSubId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The subcontarctors with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<SubcontarctorResources> subcontarctorResourcesCollectionOrphanCheck = subcontarctors.getSubcontarctorResourcesCollection();
            for (SubcontarctorResources subcontarctorResourcesCollectionOrphanCheckSubcontarctorResources : subcontarctorResourcesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Subcontarctors (" + subcontarctors + ") cannot be destroyed since the SubcontarctorResources " + subcontarctorResourcesCollectionOrphanCheckSubcontarctorResources + " in its subcontarctorResourcesCollection field has a non-nullable subcontractor field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(subcontarctors);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Subcontarctors> findSubcontarctorsEntities() {
        return findSubcontarctorsEntities(true, -1, -1);
    }

    public List<Subcontarctors> findSubcontarctorsEntities(int maxResults, int firstResult) {
        return findSubcontarctorsEntities(false, maxResults, firstResult);
    }

    private List<Subcontarctors> findSubcontarctorsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Subcontarctors.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Subcontarctors findSubcontarctors(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Subcontarctors.class, id);
        } finally {
            em.close();
        }
    }

    public int getSubcontarctorsCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Subcontarctors> rt = cq.from(Subcontarctors.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
